#!/bin/bash
# OBS 备份检查工具一键卸载脚本

# 颜色定义
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
BLUE="\033[1;34m"
CYAN="\033[1;36m"
RESET="\033[0m"

echo -e "${BLUE}=====================================${RESET}"
echo -e "${CYAN}🗑️ 华为云 OBS 备份检查工具一键卸载脚本${RESET}"
echo -e "${BLUE}-------------------------------------${RESET}"
echo -e "${YELLOW}本脚本将执行以下操作：${RESET}"
echo -e "${RED}1.${RESET} 停止所有相关的 cron 任务"
echo -e "${RED}2.${RESET} 删除 obsutil 工具"
echo -e "${RED}3.${RESET} 删除备份检查脚本"
echo -e "${RED}4.${RESET} 删除日志文件"
echo -e "${RED}5.${RESET} 删除软链接"
echo -e "${BLUE}=====================================${RESET}\n"

# 确认操作
read -p "$(echo -e "${YELLOW}⚠️  确定要卸载 OBS 备份检查工具吗？(y/n): ${RESET}")" confirm
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
    echo -e "${GREEN}取消卸载操作${RESET}"
    exit 0
fi

# 1. 停止 cron 任务
echo -e "${YELLOW}🛑 停止 cron 任务...${RESET}"
crontab -l | grep -v "check_obs_backup.sh" | crontab -
echo -e "${GREEN}✅ 已从 crontab 中移除备份检查任务${RESET}"

# 2. 删除 obsutil 工具
echo -e "${YELLOW}🗑️ 删除 obsutil 工具...${RESET}"
OBSUTIL_DIR="/usr/local/bin/obsutil"
if [ -d "$OBSUTIL_DIR" ]; then
    rm -rf "$OBSUTIL_DIR"
    echo -e "${GREEN}✅ 已删除 obsutil 目录: $OBSUTIL_DIR${RESET}"
else
    echo -e "${YELLOW}⚠️  obsutil 目录不存在: $OBSUTIL_DIR${RESET}"
fi

# 3. 删除软链接
echo -e "${YELLOW}🔗 删除软链接...${RESET}"
BIN_LINK="/usr/bin/obsutil"
if [ -L "$BIN_LINK" ]; then
    rm -f "$BIN_LINK"
    echo -e "${GREEN}✅ 已删除软链接: $BIN_LINK${RESET}"
else
    echo -e "${YELLOW}⚠️  软链接不存在: $BIN_LINK${RESET}"
fi

# 4. 删除备份检查脚本
echo -e "${YELLOW}📜 删除备份检查脚本...${RESET}"
CHECK_SCRIPT="/usr/local/bin/check_obs_backup.sh"
if [ -f "$CHECK_SCRIPT" ]; then
    rm -f "$CHECK_SCRIPT"
    echo -e "${GREEN}✅ 已删除备份检查脚本: $CHECK_SCRIPT${RESET}"
else
    echo -e "${YELLOW}⚠️  备份检查脚本不存在: $CHECK_SCRIPT${RESET}"
fi

# 5. 删除安装脚本
echo -e "${YELLOW}📜 删除安装脚本...${RESET}"
INSTALL_SCRIPT="/usr/local/bin/install_obsutil.sh"
if [ -f "$INSTALL_SCRIPT" ]; then
    rm -f "$INSTALL_SCRIPT"
    echo -e "${GREEN}✅ 已删除安装脚本: $INSTALL_SCRIPT${RESET}"
else
    echo -e "${YELLOW}⚠️  安装脚本不存在: $INSTALL_SCRIPT${RESET}"
fi

# 6. 删除日志文件（可选保留）
read -p "$(echo -e "${YELLOW}❓ 是否要删除日志文件 /var/log/obs_backup_check.log？(y/n): ${RESET}")" log_confirm
if [[ "$log_confirm" == "y" || "$log_confirm" == "Y" ]]; then
    LOG_FILE="/var/log/obs_backup_check.log"
    if [ -f "$LOG_FILE" ]; then
        rm -f "$LOG_FILE"
        echo -e "${GREEN}✅ 已删除日志文件: $LOG_FILE${RESET}"
    else
        echo -e "${YELLOW}⚠️  日志文件不存在: $LOG_FILE${RESET}"
    fi
fi

# 7. 删除配置文件（可选）
read -p "$(echo -e "${YELLOW}❓ 是否要删除 obsutil 配置文件 ~/.obsutilconfig？(y/n): ${RESET}")" config_confirm
if [[ "$config_confirm" == "y" || "$config_confirm" == "Y" ]]; then
    CONFIG_FILE="$HOME/.obsutilconfig"
    if [ -f "$CONFIG_FILE" ]; then
        rm -f "$CONFIG_FILE"
        echo -e "${GREEN}✅ 已删除配置文件: $CONFIG_FILE${RESET}"
    else
        echo -e "${YELLOW}⚠️  配置文件不存在: $CONFIG_FILE${RESET}"
    fi
fi

echo -e "\n${GREEN}🎉 OBS 备份检查工具已完全卸载！${RESET}"
echo -e "${YELLOW}注意: 本脚本不会卸载 sendmail/mailx 服务${RESET}"